// 制冷机房js 脚本 读取位置：com.mach.platform.service.IotSpaceServiceImpl.autoGenerateCoolSysVirtualDevice
dev.action = function (resp, param) {

    if (param.spaceId == undefined) {
        resp.setJsData("请传参数spaceId");
        return;
    }

    if (param.id == undefined) {
        resp.setJsData("请传参数设备ID");
        return;
    }
    //图片ID
    var PicMap = {
        '离心机': '1001',
        '螺杆机': '1002',
        '吸干机': '1003',
        '冷却塔': '1004',
        '冷却泵': '1005',
        '冷冻泵': '1006',
        '储气罐': '1007',
        '冷干机': '1008',
        '控制阀': '1010',
        '干燥机': '1011',
        '流量计': '1012',
        '温度计': '1013',
        '其他': '1999',
        '制冷主机': '1009'
    }


    // 从数据库加载对应的工艺控制配置的设备数量数据
    var deviceType = iot.ctx("iotDevServiceImpl").getStationSpaceDevices(param.spaceId);
    var workshopStation = [];
    if (deviceType['用能车间'] !=null && deviceType['车间详情'] !=null) {
        workshopStation = deviceType['车间详情'].values
    }
    //deviceType['用能车间'] = 4;

    //resp.setJsData(deviceType);
    //return;
    var result = {};
    // 请求实时属性的设备ID
    result.deviceId = param.id;
    var checkSql = "SELECT id,name FROM `iot_virtual_item` where dev_id = " + param.id;
    var dataTypeIds = iot.db().qry(checkSql);
    //获取设备数量
    //var CHILLER = selectNumberBySpaceIdAndDevName(219, "#制冷主机");
    //var COOLPUMP = selectNumberBySpaceIdAndDevName(221, "#冷却泵");
    //var COOLTOWER = selectNumberBySpaceIdAndDevName(223, "#冷却塔");
    //var FREEZPUMP = selectNumberBySpaceIdAndDevName(220, "#冷冻泵");

    var deviceData = {};
    //var deviceType = {'制冷主机': CHILLER.number, '冷却泵': COOLPUMP.number, '冷却塔': COOLTOWER.number, '冷冻泵': FREEZPUMP.number,'用能车间': 4};
    //var workshop = ['冲压车间', '涂装车间', '树脂车间', '机加车间'];
    for (var key in deviceType) {
        if (key == '制冷主机') {
            var hosts = iot.list();
            for (var i = 1; i < deviceType[key] + 1; i++) {
                var curHost = {};
                var curCtrl = {};

                curHost.name = i + '#制冷主机';

                curHost.index = i;
                curHost.pic = PicMap['制冷主机']
                curHost.status = 0;
                for (var j = 1; j <= 2 ; j++) {
                    var ctrlsChile = {};
                    ctrlsChile.index = j;
                    ctrlsChile.status = 0;
                    ctrlsChile.pic = PicMap['控制阀'];
                    if (j == 1){
                        ctrlsChile.name = i + '#制冷主机冷冻水回水电动阀';
                        curHost.left = ctrlsChile;

                    }
                    if (j == 2){
                        ctrlsChile.name = i + '#制冷主机冷却水回水电动阀';
                        curHost.right = ctrlsChile;
                    }
                    //关联属性的id，返回给前端定位
                    dataTypeIds.forAll(function (item) {
                        if (item.name.indexOf(ctrlsChile.name) > -1) {
                            ctrlsChile.id = item.id;
                        }
                    })
                }
                dataTypeIds.forAll(function (item) {
                    if (item.name.indexOf(curHost.name) > -1) {
                        curHost.id = item.id;
                    }
                })
                hosts.add(curHost);
            }
            deviceData[key] = {
                hosts: hosts
            }
        }
        else if (key == '用能车间') {
            var hosts = iot.list();
            for (var i = 0; i < workshopStation.length; i++) {
                var curStion = workshopStation[i]
                var curHost = {};

                curHost.name = curStion.devName;
                curHost.pic = PicMap['其他']
                curHost.index = i + 1;
                curHost.status = 0;
                curHost.left = {};
                curHost.right = {};
                hosts.add(curHost);
            };
            deviceData[key] = {
                hosts: hosts
            }
        }
        else {
            var hosts = iot.list();
            for (var i = 1; i < deviceType[key] + 1; i++) {
                var curHost = {};
                curHost.name = i + '#' + key;
                curHost.pic = PicMap[key]
                curHost.index = i;
                curHost.status = 0;
                curHost.left = {};
                curHost.right = {};
                dataTypeIds.forAll(function (item) {
                    if (item.name.indexOf(curHost.name) > -1) {
                        curHost.id = item.id;
                    }
                })
                hosts.add(curHost);
            };
            deviceData[key] = {
                hosts: hosts
            }
        }
    }

    // 把设备数据塞进result
    result['devices'] = deviceData;
    resp.setJsData(result);
}
